<template>
  <div>
    <div style="margin-top: 2%;text-align:center;">
      <el-select  v-model="form.courseTypeId"
                  style="width: 16%;margin-right: 1%"
                  clearable filterable
                  placeholder="请选择课程类型">
        <el-option v-for="item in courseType" :key="item.courseTypeId"
                   :label="item.courseTypeId + ' ' + item.typeName" :value="item.courseTypeId">
        </el-option>
      </el-select>
      <el-input style="width: 20%;margin-right: 1%" v-model="form.courseName"
                placeholder="请输入待查找的课程id或名称(可指定课程类型)"></el-input>
      <el-button @click="findCourse()" style="font-size: 20px" type="primary">立即查找</el-button>
      <el-button @click="addCourse()" style="font-size: 20px" type="primary">点击此处新增课程</el-button>
    </div>

    <template style="margin-top: 2%">
      <el-table :data="pageInfo.list" style="width: 100%">
        <el-table-column label="" width="80">
        </el-table-column>
        <el-table-column label="课程编号" width="200" prop="courseId" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.courseId}}</span>
          </template>
        </el-table-column>
        <el-table-column label="课程名称" width="480" prop="courseName" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.courseName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="课程类型" width="360" prop="typeName" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.typeName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="学分" width="150" prop="credit" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.credit}}</span>
          </template>
        </el-table-column>
        <el-table-column label="学时" width="150" prop="period" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.period}}</span>
          </template>
        </el-table-column>
        <el-table-column label="课程属性" width="120" prop="flag" sortable>
          <template slot-scope="scope">
            <span v-if="scope.row.flag===1">必修</span>
            <span v-if="scope.row.flag===2">限选</span>
            <span v-if="scope.row.flag===3">选修</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button  size="mini" type="primary" style="font-size: 16px"
                        @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-bottom: 1%;"></div>
      <el-pagination
          style="text-align: center;"
          background
          layout="total,prev, pager, next"
          :total="pageInfo.total"
          :page-size="pageInfo.pageSize"
          @current-change="handleCurrentChange">
      </el-pagination>
    </template>

    <el-dialog title="新增/编辑课程" :visible.sync="dialogForm" :before-close="handleClose">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="20%">
        <el-form-item  label="请输入课程编号" prop="courseId">
          <el-input style="width: 40%;"
                    placeholder="请输入目标课程编号"
                    v-model="ruleForm.courseId"
                    oninput ="value=value.replace(/[^0-9]/g,'')"></el-input>
        </el-form-item>
        <el-form-item  label="请输入课程名称" prop="courseName">
          <el-input style="width: 60%;" v-model="ruleForm.courseName"
                    placeholder="请输入课程名称"></el-input>
        </el-form-item>
        <el-form-item  label="请选择课程类型" prop="courseTypeId">
          <el-select  v-model="ruleForm.courseTypeId"
                      style="width: 40%;"
                      clearable filterable
                      placeholder="请选择课程类型">
            <el-option v-for="item in courseType" :key="item.courseTypeId"
                       :label="item.courseTypeId + ' ' + item.typeName" :value="item.courseTypeId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="请输入课程学分" prop="credit">
          <el-input   style="width: 30%;"
                      placeholder="请输入课程学分"
                      v-model="ruleForm.credit"
                      oninput ="value=value.replace(/[^0-9.]/g,'')"
                      :blur="checkCredit(ruleForm.credit)">
          </el-input>
        </el-form-item>
        <el-form-item  label="请输入课程学时" prop="period">
          <el-input style="width: 30%;"
                    placeholder="请输入课程学时"
                    v-model="ruleForm.period"
                    oninput ="value=value.replace(/[^0-9]/g,'')">
          </el-input>
        </el-form-item>
        <el-form-item label="请选择课程属性" prop="flag">
          <el-select v-model="ruleForm.flag" style="width: 20%" placeholder="请选择课程属性">
            <el-option label="必修" :value="1"/>
            <el-option label="限选" :value="2"/>
            <el-option label="选修" :value="3"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return {
      dialogForm: false,
      form:{
        courseTypeId:'',
        courseName:'',
      },
      courseType:[],
      currPage: 1,
      pageInfo:{},
      ruleForm:{
        oldCourseId: '',
        courseId: '',
        courseName: '',
        courseTypeId: '',
        credit: '',
        period: '',
        flag: '',
      },
      rules:{
        courseId: [
          { required: true, message: '请输入课程编号', trigger: 'blur' },
          { min: 2, max: 8, message: '请输入2 - 8位数字!', trigger: 'blur' }
        ],
        courseName: [
          { required: true, message: '请输入课程名称', trigger: 'blur' },
          { min: 2, max: 30, message: '请输入2 - 30个字符!', trigger: 'blur' }
        ],
        courseTypeId: [
          { required: true, message: '请选择课程类型', trigger: 'blur' },
        ],
        credit: [
          { required: true, message: '请输入课程学分', trigger: 'blur' },
        ],
        period: [
          { required: true, message: '请输入课程学时', trigger: 'blur' },
          { min: 1, max: 4, message: '请输入1 - 4位数字!', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.initCourseType();
    this.getData();
  },
  methods:{
    submitForm(formName){ // 新增/修改课程
      this.$refs[formName].validate((valid) => {//校验格式
        if (valid) {//格式校验通过
          let path = `http://localhost:8081/course/updateCourse/${this.ruleForm.oldCourseId}`;
          this.$ajax.post(path,this.ruleForm).then(res=>{
            if(res.data.status === 0){
              this.$message.success(res.data.message);
              this.dialogForm = false;
              this.getData();
            }
            else{
              this.$message.error(res.data.message);
            }
          })
        }
      });
    },
    handleEdit(row){
      this.ruleForm.oldCourseId = row.courseId;
      this.ruleForm.courseId = row.courseId + '';
      this.ruleForm.courseName = row.courseName;
      this.ruleForm.courseTypeId = row.courseTypeId;
      this.ruleForm.credit = row.credit;
      this.ruleForm.period = row.period + '';
      this.ruleForm.flag = row.flag;
      this.dialogForm = true;
    },
    addCourse(){
      this.ruleForm.oldCourseId = -1;
      this.ruleForm.courseId = '';
      this.ruleForm.courseName = '';
      this.ruleForm.courseTypeId = '';
      this.ruleForm.credit = '';
      this.ruleForm.period = '';
      this.ruleForm.flag = 1;
      this.dialogForm = true;
    },
    findCourse(){
      this.currPage = 1;
      this.getData();
    },
    handleCurrentChange(val){//翻页
      this.currPage = val;
      this.getData();
    },
    initCourseType(){
      let path = 'http://localhost:8081/course/getAllType';
      this.$ajax.post(path).then(res=>{
        this.courseType = res.data.list
      })
    },
    checkCredit(input){//学分数值校验
      if(isNaN(input)){
        this.ruleForm.credit = '';
        this.$message.error('您输入的学分为非数字!');
      }
      else if(input > 9999999.0){
        this.ruleForm.credit = 9999999.0;
        this.$message.error('您输入的学分数值过大!');
      }
    },
    getData(){
      let path = `http://localhost:8081/course/getCourseList/${this.currPage}`;
      this.$ajax.post(path,this.form).then(res=>{
        this.pageInfo = res.data;
      })
    },
    handleClose(done){
      this.$confirm('确认关闭?').then(()=>{done();})
    }
  }
}
</script>

<style scoped>
span{
  font-size: 18px;
}
</style>